﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace BackToCOS
{
    public class Util
    {
        /// <summary>
        /// HMAC-SHA1算法
        /// </summary>
        /// <param name="key"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string HmacSha1(string key, string content)
        {
            var buff = new HMACSHA1(Encoding.UTF8.GetBytes(key)).ComputeHash(Encoding.UTF8.GetBytes(content));
            StringBuilder sb = new StringBuilder();
            foreach (byte Byte in buff)
            {
                sb.AppendFormat("{0:x2}", Byte);
            }
            return sb.ToString();
        }
        /// <summary>
        /// Sha1算法
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Sha1(string content)
        {
            var buff = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(content));
            StringBuilder sb = new StringBuilder();
            foreach (byte Byte in buff)
            {
                sb.AppendFormat("{0:x2}", Byte);
            }
            return sb.ToString();
        }
        /// <summary>
        /// unix时间戳（秒）
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static long ToUnixTime(DateTime dateTime)
        {
            return (dateTime.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        }
    }
}
